//
//  JHRefreshHeaderView.swift
//  SwiftTest
//
//  Created by 李江湖 on 2018/8/2.
//  Copyright © 2018年 李江湖. All rights reserved.
//

import UIKit

class JHHeaderView: UIView {
    lazy var imageView : UIImageView = {
        let imageV = UIImageView(image: UIImage(named: "dropdown_anim__0001"))
        
        if UIScreen.main.bounds.size.width == 812.0{
            imageV.frame = CGRect(x:(ScreenWidth-90)/2 , y: 44, width: 90, height: 70)
        }else{
            imageV.frame = CGRect(x:(ScreenWidth-90)/2 , y: 0, width: 90, height: 70)
            
        }
        return imageV
    }()
    lazy var Activity : UIActivityIndicatorView = {
        let imageV = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
        
//        imageV.frame = CGRect(x:CGRect., y: <#T##CGFloat#>, width: <#T##CGFloat#>, height: <#T##CGFloat#>)
        if UIScreen.main.bounds.size.width == 812.0{
            imageV.frame = CGRect(x:(ScreenWidth-90)/2 , y: 44, width: 90, height: 70)
        }else{
            imageV.frame = CGRect(x:(ScreenWidth-90)/2 , y: 0, width: 90, height: 70)
            
        }
        return imageV
    }()
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.creatUI()
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.creatUI()
    }
    func creatUI() {
        self.addSubview(self.imageView)
    }
}
class JHRefreshHeaderView: MJRefeshHeader {
    var autoHeaderView = JHHeaderView.init(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 70))
    var animationImageArray = [UIImage]()
    
    override func prepare() {
        super.prepare()
        if UIScreen.main.bounds.size.height == 812.0 {
            self.mj_height = 70 + 44
        } else {
            self.mj_height = 70
        }
        
        self.addSubview(self.autoHeaderView)
        for i in 0...60 {
            if let image = UIImage(named: String(format: "dropdown_anim__000%zd", i)) {
                self.animationImageArray.append(image)
            }
        }
    }
    
    override func placeSubviews() {
        super.placeSubviews()
        var statusHeight : CGFloat = 0.0
        if UIScreen.main.bounds.size.width == 812.0 {
            statusHeight =  44.0
        } else {
            statusHeight = 0.0
        }
        self.autoHeaderView.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: 70+statusHeight)
        
    }
    override func scrollViewContentOffsetDidChange(change: [NSKeyValueChangeKey : Any]?) {
        super.scrollViewContentOffsetDidChange(change: change)
    }
    
    override var pullingPercent: CGFloat{
        didSet{
            if self.state != .idle {
                return
            }
            self.autoHeaderView.imageView.stopAnimating()
            self.autoHeaderView.imageView.image = self.animationImageArray.first
        }
    }
    
    override var state: MJRefreshState {
        didSet{
            if state == .pulling {
                self.autoHeaderView.imageView.stopAnimating()
                self.autoHeaderView.imageView.image = self.animationImageArray.first
            }else if state == .refreshing  {
                self.autoHeaderView.imageView.stopAnimating()
                self.autoHeaderView.imageView.animationImages = self.animationImageArray
                let time: Double = Double(self.animationImageArray.count) * 0.01
                self.autoHeaderView.imageView.animationDuration = time
                self.autoHeaderView.imageView.startAnimating()
            }else if state == .idle{
//                self.gifView.isHidden = false
                self.autoHeaderView.imageView.stopAnimating()
                self.autoHeaderView.imageView.image = self.animationImageArray.first
            } else if state == .noMoreData  {
//                self.gifView.isHidden = true
            }
        }
    }
    

}
